//
//  NetManager.m
//  网络记事本
//
//  Created by wukeng on 16/4/2.
//  Copyright © 2016年 wukeng. All rights reserved.
//

#import "NetManager.h"

@implementation NetManager
//添加记事的方法
+(void)AddDataWithContent:(NSString *)content
                 WithData:(NSString *)time
         WithSuccessBlock:(void(^)(NSData *data))successBlock
            WithFailBlock:(void(^)(NSError *error))failBlock{
    // 请求的网址
    NSURL *url = [NSURL URLWithString:@"http://www.51work6.com/service/mynotes/WebService.php?email=wukeng234@126.com&type=JSON&"];
    //请求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //设置为POST
    request.HTTPMethod = @"POST";
    //设置请求体
    NSString *bodyStr = [NSString stringWithFormat:@"action=add&content=%@&date=%@",content,time];
    request.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
                               
                               if (connectionError == nil) {
                                   NSError *error;
                                   NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
                                   if ([dict[@"ResultCode"] intValue] == 1) {
                                       successBlock(data);
                                   }
                                   else{
                                       failBlock(error);
                                   }
                                   
                               }
                               else{
                                   failBlock(connectionError);
                               }
                           }];
    
}
//查询记事的方法
+(void)getDataWithSuccessBlock:(void(^)(NSArray *arr))successBlock
                 WithFailBlock:(void(^)(NSError *error))failBlock{
    NSURL *url = [NSURL URLWithString:@"http://www.51work6.com/service/mynotes/WebService.php?email=wukeng234@126.com&type=JSON&action=query"];
    //请求
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //发送请求 得到数据
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
                               if (connectionError == nil) {
                                   
                                   NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                   NSLog(@"%@",str);
                                   NSError *error;
                                   NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data
                                                                                        options:NSJSONReadingMutableLeaves
                                                                                          error:&error];
                                  
                                   if ([error localizedDescription] == nil) {
                                       NSArray *arr = dict[@"Record"];
                                       successBlock(arr);
                                   }
                                   else{
                                       failBlock(error);
                                   }
                               }
                               else{
                                   failBlock(connectionError);
                               }
                           }];
    
}


//删除记事的方法
+(void)deleteNoteWithNoteID:(int)noteID{
    NSURL *url = [NSURL URLWithString:@"http://www.51work6.com/service/mynotes/WebService.php?email=wukeng234@126.com&type=JSON&"];
    NSMutableURLRequest *mutRequest = [NSMutableURLRequest requestWithURL:url];
    mutRequest.HTTPMethod = @"POST";
    NSString *bodyStr = [NSString stringWithFormat:@"action=remove&id=%i",noteID];
    mutRequest.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    
    [NSURLConnection sendAsynchronousRequest:mutRequest
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
                               nil;
                           }];
}


@end
















